package com.codejoys.monitor.web;

import com.codejoys.monitor.domain.Config;
import com.codejoys.monitor.domain.SysConfig;
import com.codejoys.monitor.domain.User;
import com.codejoys.monitor.domain.VoltageModeConfig;
import com.codejoys.monitor.dto.BaseResult;
import com.codejoys.monitor.dto.SysUser;
import com.codejoys.monitor.service.ConfigService;
import com.codejoys.monitor.service.UserService;
import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
public class ConfigController {
    @Autowired
    ConfigService configService;

    @Autowired
    UserService userService;

    @GetMapping("/configs")
    public BaseResult findAll(@RequestParam("house") String house) {
        final List<Config> data = configService.findAll(house);

        return BaseResult.builder()
            .success()
            .data(data)
            .build();
    }

    @DeleteMapping("/configs/{id}")
    public BaseResult deleteById(@PathVariable("id") Integer id) {
        configService.deleteById(id);

        return BaseResult.success();
    }

    @PutMapping("/configs/{id}")
    public BaseResult update(@PathVariable("id") Integer id, Config config, Authentication auth) {
        String name = auth.getName();
        final SysUser user = userService.findUserByName(name);

        config.setUpdated(new Date());
        config.setUpdatedBy(user.getId());

        config.setId(id);
        configService.update(config);

        return BaseResult.success();
    }

    @PostMapping("/configs")
    public BaseResult insert(Config config, Authentication auth) {
        String name = auth.getName();
        final SysUser user = userService.findUserByName(name);

        config.setCreated(new Date());
        config.setCreatedBy(user.getId());

        configService.insert(config);

        return BaseResult.success();
    }

    @PostMapping("/voltageModeConfigs")
    public BaseResult insertVoltageMode(VoltageModeConfig config) {
        configService.insertVoltageMode(config);

        return BaseResult.success();
    }

    @GetMapping("/voltageModeConfigs")
    public BaseResult findAllVoltageMode() {
        final List<VoltageModeConfig> data = configService.findAllVoltageModeConfig();

        return BaseResult.builder()
                .success()
                .data(data)
                .build();
    }

    @DeleteMapping("/voltageModeConfigs/{id}")
    public BaseResult deleteVoltageModeById(@PathVariable("id") Integer id) {
        configService.deleteVoltageModeById(id);

        return BaseResult.success();
    }

    @PutMapping("/voltageModeConfigs/{id}")
    public BaseResult updateVoltageMode(@PathVariable("id") Integer id, VoltageModeConfig config) {
        config.setId(id);
        configService.updateVoltageMode(config);

        return BaseResult.success();
    }

    @PostMapping("/sysConfigs")
    public BaseResult insertSysConfig(@RequestBody List<SysConfig> configs) {
        configService.insertSysConfigs(configs);

        return BaseResult.success();
    }

    @GetMapping("/sysConfigs")
    public BaseResult findAllSysConfig() {
        final List<SysConfig> data = configService.findAllSysConfig();

        return BaseResult.builder()
                .success()
                .data(data)
                .build();
    }

    @DeleteMapping("/sysConfigs/{id}")
    public BaseResult deleteSysConfigById(@PathVariable("id") Integer id) {
        configService.deleteSysConfigById(id);

        return BaseResult.success();
    }

    @PutMapping("/sysConfigs/{id}")
    public BaseResult updateSysConfigs(@PathVariable("id") Integer id, SysConfig config) {
        config.setId(id);
        configService.updateSysConfig(config);

        return BaseResult.success();
    }
}
